package com.sunbeam.common.utils.encrypt;

import java.security.MessageDigest;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;


/**
 * Base Encrypt Components
 * @ClassName: Coder
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/09/17 14:04:52
 */

public abstract class Coder {
	public static final String KEY_SHA = "SHA";
	public static final String KEY_MD5 = "MD5";

	/**
	 * MAC Algorithms
	 * 
	 * <pre>
	 * HmacMD5 
	 * HmacSHA1 
	 * HmacSHA256 
	 * HmacSHA384 
	 * HmacSHA512
	 * </pre>
	 */
	public static final String KEY_MAC = "HmacMD5";

	/**
	 * 
	 * @Title: decryptBASE64
	 * @Description: BASE64 Decrypt
	 * @param @param key
	 * @param @return
	 * @param @throws Exception
	 * @return byte[]
	 * @throws
	 */
	@SuppressWarnings("static-access")
	public static byte[] decryptBASE64(String key) throws Exception {
		return (new Base64()).decodeBase64(key);
	}

	/**
	 * 
	 * @Title: encryptBASE64
	 * @Description: BASE64 Encrypt
	 * @param @param key
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("static-access")
	public static String encryptBASE64(byte[] key) throws Exception {
		return (new Base64()).encodeBase64String(key);
	}

	/**
	 * 
	 * @Title: encryptMD5
	 * @Description: MD5 Encrypt
	 * @param @param data
	 * @param @return
	 * @param @throws Exception
	 * @return byte[]
	 * @throws
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {

		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
		md5.update(data);

		return md5.digest();

	}

	/**
	 * 
	 * @Title: encryptSHA
	 * @Description: SHA Encrypt
	 * @param @param data
	 * @param @return
	 * @param @throws Exception
	 * @return byte[]
	 * @throws
	 */
	public static byte[] encryptSHA(byte[] data) throws Exception {

		MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
		sha.update(data);

		return sha.digest();

	}

	/**
	 * 
	 * @Title: initMacKey
	 * @Description: Initialize HMAC key
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	public static String initMacKey() throws Exception {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
		SecretKey secretKey = keyGenerator.generateKey();
		return encryptBASE64(secretKey.getEncoded());
	}

	
	/**
	 * 
	 * @Title: encryptHMAC
	 * @Description: HMAC Encrypt
	 * @param @param data
	 * @param @param key
	 * @param @return
	 * @param @throws Exception
	 * @return byte[]
	 * @throws
	 */
	public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
		SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		mac.init(secretKey);
		return mac.doFinal(data);
	}
}
